$(function () {

    let $title = $("#news-title");
    let $desc = $("#news-desc");
    let $imageUrl = $("#news-thumbnail-url");
    let $videoUrl = $("#docs-file-url");
    let $tag = $("#course-category");
    let $teacher = $("#course-teacher");
    let $content = $("#content");
    let $pubBtn = $("#btn-pub-news");

    // 上传图片
    let $uploadImageBtn = $("#upload-image-server");
    $uploadImageBtn.change(function () {
        let image_file = this.files[0];
        let form = new FormData();
        form.append("image_file", image_file);
        $.ajax({
            url: "/x-admin/news/image/upload/",
            data: form,
            type: "POST",
            processData: false,
            contentType: false,
        }).done(function (res) {
            if (res.errno === "0") {
                let $imageUrl = $("#news-thumbnail-url");
                $imageUrl.val(res.data.image_url);
                fAlert.alertSuccessToast("上传成功");
                return;
            } else {
                fAlert.alertErrorToast(res.errmsg);
            }
        }).fail(function (err) {
            fAlert.alertErrorToast("网络超时，请重试！！！");
        })
    });
    // 上传视频
    let sdk = baidubce.sdk;
    // let sdk = require("@baiducloud/sdk");
    let VodClient = sdk.VodClient;
    const config = {
        endpoint: 'http://vod.bj.baidubce.com',
        credentials: {
            ak: 'd7b0528d41804aca885b7ad4cd189d78',
            sk: 'e63ec63116ee41ffbb0d11e652e14f18',
        },
    };
    let client = new VodClient(config);
    const DOMAIN = "kdutezswmddfzihx3j6.exp.bcevod.com";
    let $upload_file_server = $("#upload-file-server");
    $upload_file_server.change(function () {
        let title = $title.val();
        if(!title){
            fAlert.alertErrorToast("请先填写课程标题之后，再上传视频！");
            $(this).val("");
            return;
        }
        let desc = $desc.val();
        if(!desc){
            $(this).val("");
            fAlert.alertErrorToast("请先填写课程描述之后，再上传视频！");
            return;
        }
        let video_file = this.files[0];
        console.log(video_file);
        let video_type = video_file.type;
        let blob = new Blob([video_file], {type: video_type});
        // video_file = {lastModified: 123433124535422, lastModifiedDate: "", name: "adv.mp4", size: 123212, type: "video/mp4"}
        console.log(video_file);

        client.createMediaResource(title, desc, blob)
            .then(function (response) {
                let mediaId = response.body.mediaId;
                console.log(mediaId);
                let accessUrl = "http://" + DOMAIN + "/" + mediaId + "/" + mediaId + ".m3u8";
                $videoUrl.val(accessUrl);
                fAlert.alertSuccessToast("上传成功");
            })
            .catch(function (error) {
                fAlert.alertErrorToast(error);
            });
    });

    // 发布文章
    $pubBtn.click(function(){
        if(!$title.val()){
            fAlert.alertErrorToast("请填写课程标题");
            return;
        }
        if(!$desc.val()){
            fAlert.alertErrorToast("请填写课程简介");
            return;
        }
        if(!$imageUrl.val()){
            fAlert.alertErrorToast("请上传课程缩略图");
            return;
        }
        if(!$videoUrl.val()){
            fAlert.alertErrorToast("请上传视频或输入视频地址");
            return;
        }
        if(!$teacher.val() || $teacher.val()==="0"){
            fAlert.alertErrorToast("请选择课程讲师");
            return;
        }
        if(!$tag.val()||$tag.val()==="0"){
            fAlert.alertErrorToast("请选择课程分类");
            return;
        }
        if(!$content.val()){
            fAlert.alertErrorToast("请填写课程大纲");
            return;
        }
        let data = {
            title: $title.val(),
            cover_url: $imageUrl.val(),
            video_url: $videoUrl.val(),
            profile: $desc.val(),
            outline: $content.val(),
            teacher: $(this).data(""),
            category: $(this).data(""),

        }
        console.log(data);
        $.ajax({
            url: "/x-admin/course/pub/",
            type: "post",
            data: JSON.stringify(data),
            contentType: "application/json",
            dataType: "json"
        }).done(function () {
            if(res.errno==="0"){
                fAlert.alertSuccessToast("发布成功，即将跳转到管理页面...");
                setTimeout(()=>{
                    window.location.href = "http://www.blogcloud.info/x-admin/course/";
                }, 1000);
            }
            else{
                fAlert.alertErrorToast(res.errmsg)
            }
        }).fail(()=>{
            fAlert.alertErrorToast("网络超时，请刷新重试");
        })

    });

});
